1689A - Lex String - CodeForces Solution


brute force greedy implementation sortings two pointers *800

Please click on ads to support us..

Python Code:

from sys import stdin
input = stdin.readline

from math import ceil, floor, sqrt, log2
from heapq import heappush, heappop
from collections import deque
from functools import lru_cache
from bisect import bisect_left, bisect_right

def rl(t = int):
    return list(map(t, input().split()))

T = int(input())

for t in range(1, T + 1):
    n, m, k = rl()
    a = input().strip()
    b = input().strip()

    sa = sorted(a)
    sb = sorted(b)

    ret = []
    i = j = 0
    row = [0, 0]
    while i < n and j < m:
        if (sb[j] < sa[i] or row[0] == k) and row[1] < k:
            ret.append(sb[j])
            row = [0, row[1] + 1]
            j += 1
        else:
            ret.append(sa[i])
            row = [row[0] + 1, 0]
            i += 1

    print(''.join(ret))

C++ Code:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int n, m, k;
string a, b, s;
int c1[26], c2[26];

void solution() {
    cin >> n >> m >> k >> a >> b;
    for(int i = 0;i < n;i++) {
        c1[ a[i] - 97 ]++;
    }
    for(int j = 0;j < m;j++) {
        c2[ b[j] - 97 ]++;
    }

//    for(int i =0 ;i < 26;i++) cout << c1[i] << ' '; cout << '\n';
//    for(int i =0 ;i < 26;i++) cout << c2[i] << ' '; cout << '\n';

    int I = 0, J = 0;
    int cnt = 0, last = -1;
    while(!c1[I]) I++;
    while(!c2[J]) J++;

    while(n && m) {

        if(last == 1) {
            if(I < J && cnt != k) {
                s += I + 97;
                c1[I]--;
                while(!c1[I] && I < 26) I++;
                cnt++;
                n--;
            }
            else {
                s += J + 97;
                c2[J]--;
                while(!c2[J] && J < 26) J++;
                cnt = 1;
                last = 2;
                m--;
            }
        }
        else if(last == 2) {
            if(J < I && cnt != k){
                s += J + 97;
                c2[J]--;
                while(!c2[J] && J < 26) J++;
                cnt++;
                m--;
            }
            else {
                s += I + 97;
                c1[I]--;
                while(!c1[I] && I < 26) I++;
                cnt = 1;
                last = 1;
                n--;
            }
        }
        else {
            if(I < J) {
                s += I + 97;
                c1[I]--;
                while(!c1[I] && I < 26) I++;
                cnt = 1;
                last = 1;
                n--;
            }
            else {
                s += J + 97;
                c2[J]--;
                while(!c2[J] && J < 26) J++;
                cnt = 1;
                last = 2;
                m--;
            }
        }
    }

    cout << s << '\n';
//    cout << "\n\n";
}

int main() {
    //freopen("samples.txt", "r", stdin);
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

    int t = 1;
    cin >> t;
    while(t--) {
        solution();
        for(int i = 0;i < 26;i++) {
            c1[i] = 0;
            c2[i] = 0;
        }
        s.clear();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math